import { addCart,getProductDetail } from '../../../../common/api/index'
Page({
    data: {
		indicatorDots: true,
		vertical: false,
		autoplay: true,
		interval: 4000,
		duration: 1000,
		goodsList:[
			'http://8.149.131.154/goods.jpg',
			'http://8.149.131.154/goods.jpg',
			'http://8.149.131.154/goods.jpg',
			'http://8.149.131.154/goods.jpg',
		],
		showSkuBool: false,//sku弹窗
		buyCount: 1,//购买数量
		submitTxt: '立即购买',//提交按钮名称
		id: null,//商品id
		goodsInfo:{},//商品信息
		skuInfo: {},
		current: 0,//规格选择
	},
    onLoad(options) {
		if(options.id){
			this.setData({
				id: options.id
			})
			this.getGoodsDetailInfo()
		}
	},
	// 数量更改
	onChangeNum(e){
		this.setData({
			buyCount: e.detail
		})
	},
	// 点击规格
	clickSpec(e){
		let skuid = e.currentTarget.dataset.id;
		this.data.goodsInfo.skuList.forEach((item,index) => {
			if(item.id == skuid){
				this.setData({
					current: index,
					skuInfo: item
				})
			}
		})
	},
	// 加载商品详情
	getGoodsDetailInfo(){
		getProductDetail(this.data.id).then(res => {
			if(res.code == 200){
				res.data.productDetail = this.goodsContentConv(res.data.productDetail)
				this.setData({
					goodsInfo: res.data
				})
			}
		})
	},
	// 处理富文本图片样式  商品详情html正则追加max-width
	goodsContentConv(content){
		return content.replace(/<(img).*?(\/>|<\/img>)/g, function (mats) {
			if (mats.indexOf('style') < 0) {
				return mats.replace(/<\s*img/, '<img style="max-width:100%;height:auto;"');
			} else {
				return mats.replace(/style=("|')/, 'style=$1max-width:100%;height:auto;')
			}
		});
	},
	// 关闭sku弹窗
	closeSkuPopup(){
		this.setData({
			showSkuBool: false
		})
	},
	// 显示sku弹窗
	toShowSku(e){
		let type = e.target.dataset.type;
		let skuInfo,current;
		if(this.data.current != 0){
			skuInfo = this.data.goodsInfo.skuList[current];
			current = this.data.current
		}else{
			skuInfo = this.data.goodsInfo.skuList[0];
			current = 0
		}
		if(type == 'addCart'){
			this.setData({
				showSkuBool: true,
				current: current,
				submitTxt:'加入购物车',
				skuInfo:skuInfo
			})
		}else{
			this.setData({
				showSkuBool: true,
				submitTxt:'立即购买',
				current: current,
				skuInfo:skuInfo
			})
		}
	},
	toSubmit(){
		this.setData({
			showSkuBool: false
		})
		if(this.data.submitTxt == '加入购物车'){
			// this.data.skuInfo.id,//商品id
			// this.data.buyCount,//商品数量
			let str = "?skuId="+this.data.skuInfo.id+'&buyCount='+this.data.buyCount;
			addCart(str).then(res => {
				if(res.code == 200){
					wx.showToast({
						title: '加入购物车成功',
						icon: 'none',
						duration: 2000
					})
				}
			})
		}else{
			let goods = this.data.skuInfo
			goods.buyCount = this.data.buyCount;
			wx.setStorageSync('goodsInfo', [goods])
			wx.removeStorageSync('chooeseAddrressData')
			wx.navigateTo({
				url: '/pages/component/classify/submitOrder/index'
			})
		}
	},
	toCart(){
		wx.switchTab({
			url: '/pages/tabbar/cart/index/index'
		})
	},

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})